/*
 * GAbstractAdminCommand.java
 * Created on 4 oct. 2008
 *
 * Glacéo Internet Platform
 * http://sourceforge.net/projects/chm/
 *
 * Copyright (c) 2005-2008, Jens and Jan Janke (VirtualHockey Project)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.hockeo.vhadmin;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import com.hockeo.vhbase.data.dao.IGDao;
import com.hockeo.vhbase.error.GException;
import com.hockeo.vhbase.seasoncycle.GSeasonServiceAdapter;

/**
 * Abstract base class offering some service methods for administration console commands.
 *
 * @version $Id$
 * @author jjanke
 */
public abstract class GAbstractAdminCommand implements IGAdminCommand
{
  private IGDao            d_dao;
  private GSeasonServiceAdapter d_seasonServiceAdapter;
  private BufferedReader        d_br;

  public void setDao( IGDao dao )
  {
    d_dao = dao;
  }

  protected IGDao getDao()
  {
    return d_dao;
  }

  public void setSeasonServiceAdapter( GSeasonServiceAdapter seasonServiceAdapter )
  {
    d_seasonServiceAdapter = seasonServiceAdapter;
  }

  /**
   * Displays a default usage string that only mentions the command short cut.
   */
  public void showUsageHelp()
  {
    System.out.println( "Command syntax is wrong." );
    System.out.println( "Usage: " + getShortCut() );
  }

  protected GSeasonServiceAdapter getSeasonServiceAdapter()
  {
    return d_seasonServiceAdapter;
  }

  protected BufferedReader getConsoleReader()
  {
    if ( d_br == null )
      d_br = new BufferedReader( new InputStreamReader( System.in ) );

    return d_br;
  }

  protected String readLineFromConsole() throws GException
  {
    try
    {
      return getConsoleReader().readLine();
    }
    catch ( IOException ex )
    {
      throw new GException( "Failed reading user input from the console.", ex );
    }
  }

  protected boolean shouldProceed() throws GException
  {
    System.out.print( "Do you want to proceed (y/n)? " );

    String strAnswer = readLineFromConsole();

    return !strAnswer.toLowerCase().startsWith( "n" );
  }
}
